#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct stu
{
    int number;
    int grade;
};
bool cmp(stu& s1, stu s2)
{
    if (s1.grade != s2.grade)
        return s1.grade < s2.grade;

    return s1.number < s2.number;
}

int main()
{
    int n;
    vector<stu> vs;

    while (cin >> n)
    {
        vs.reserve(n + 1);
        for (int i = 0; i < n; i++)
        {
            stu temp;
            cin >> temp.number >> temp.grade;
            vs.push_back(temp);
        }
        sort(vs.begin(), vs.end(), cmp);
        for (int i = 0; i < n; i++)
        {
            cout << vs[i].number << " " << vs[i].grade << endl;
        }
    }

    return 0;
}


